/**
 * @param {number[]} arr
 * @param {number} k
 * @return {number}
 */
export var findKthPositive = function (arr, k) {
  if (arr[0] > k) return k
  let ansIdx = -1
  let [left, right] = [0, arr.length - 1]
  while (left <= right) {
    const mid = (left + right) >> 1
    const diff = arr[mid] - mid - 1
    if (diff >= k) {
      ansIdx = mid
      right = mid - 1
    } else {
      left = mid + 1
    }
  }
  if (ansIdx === -1) return k + arr.length
  return k + ansIdx
}
